<?php
// +----------------------------------------------------------------------
// | 07FLYSAAS [基于ThinkPHP5.0开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2025-2035 http://www.07fly.xyz
// +----------------------------------------------------------------------
// | Professional because of focus  Persevering because of happiness
// +----------------------------------------------------------------------
// | Author: 开发人生 <goodkfrs@qq.com>
// +----------------------------------------------------------------------


namespace app\plat\model;

/**
 * 编码规则模型
 */
class TenSeqnum extends PlatBase
{
    public function getTenSeqnum($name, $isupdate = false)
    {
        $time = time();
        $re = '';
        //$info = db('sys_seqnum')->where(['name' => $name])->find();
        $info = $this->modelTenSeqnum->getInfo(['name' => $name]);
        //先判断是否存在，如果不存在，调用默认的配置
        if (empty($info['enable'])) {
            //默认
            $default = [
                'salcontract' => ['pre' => 'XSHD', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'salorder' => ['pre' => 'XSDD', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'sale' => ['pre' => 'XH', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'resale' => ['pre' => 'XHTH', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'quoted' => ['pre' => 'BJD', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'poscontract' => ['pre' => 'CGHD', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'stockinto' => ['pre' => 'DB', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'stockout' => ['pre' => 'CK', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'stockcheck' => ['pre' => 'PD', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'stockchange' => ['pre' => 'DB', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'stockassemble' => ['pre' => 'ZZ', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'finpayrecord' => ['pre' => 'FK', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'finrecerecord' => ['pre' => 'HK', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'fininvoicepay' => ['pre' => 'KP', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'fininvoicerece' => ['pre' => 'SP', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'finremiburs' => ['pre' => 'BX', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'exchange' => ['pre' => 'JFDH', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'zxorder' => ['pre' => 'DD', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'zxteam' => ['pre' => 'ZT', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
                'zxtransferteam' => ['pre' => 'FT', 'y' => 1, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0, 'suffix' => '', 'nums' => '1', 'len' => '3', 'enable' => 1],
            ];
            //编号不可为空的模块
            if (isset($default[$name])) {
                $info = $default[$name];//替换默认
            }
        }
        //再次判断启用情况
        if (!empty($info['enable'])) {
            //字符前缀
            $re .= $info['pre'];

            //日期
            !empty($info['y']) && $re .= date('Y', $time);
            !empty($info['m']) && $re .= date('m', $time);
            !empty($info['d']) && $re .= date('d', $time);
            !empty($info['h']) && $re .= date('H', $time);
            !empty($info['i']) && $re .= date('i', $time);
            !empty($info['s']) && $re .= date('s', $time);

            //数字,是否启用数字
            if (!empty($info['nums'])) {

                //字符后缀
                !empty($info['suffix']) && $re .= $info['suffix'];

                //更新编码规则自增
                $isupdate && $this->setSequenceNums($name, $re, $info['nums']);

                //获取模块开始数字
                $nums = $this->getSequenceNums($name, $re);
                if (!empty($info['len'])) {
                    $re .= str_pad($nums, $info['len'], '0', STR_PAD_LEFT);
                } else {
                    $re .= $nums;
                }
            }
        }
        return $re;
    }
    
    //获取自增编码
    public function getSequenceNums($name = '', $prekey = '', $value = 1)
    {
        $db = db('sequence');
        $info = $db->where(['name' => $name, 'current_date' => $prekey])->find();
        if (!empty($info['current_value'])) {
            $value = $info['current_value'];
        } else {
            $intodata = ['name' => $name, 'current_value' => $value, 'current_date' => $prekey];
            $db->insert($intodata);
        }
        return $value;
    }

    public function setSequenceNums($name = '', $prekey = '')
    {
        $db = db('sequence');
        $where = ['name' => $name, 'current_date' => $prekey];
        $info = $db->where($where)->find();
        if (!empty($info['current_value'])) {
            $db->where($where)->setInc('current_value');
        }
    }
}
